import datetime
import decimal

from django.shortcuts import render,redirect
from django.http import HttpResponse
from student.models import Student,School
from decimal import Decimal
from django.views import View
from django.db.models import Q
# Create your views here.


def student_show(request):
    cookie=request.COOKIES.get('mycookie')
    session=request.session.get('id')
    print(session,cookie)

    school_data=School.objects.all()
    stu_name=request.GET.get('name','')
    sch_name=request.GET.get('school','')
    query=Q()
    if stu_name:
        query=query & Q(name__contains=stu_name)
    if sch_name:
        query=query & Q(school_id=int(sch_name))

    student_data=Student.objects.filter(query)


    return render(request,'show.html',locals())



class Student_Add(View):
    def get(self,request):
        school_data=School.objects.all()

        return render(request, 'add.html', locals())

    def post(self,request):
        print(request.POST)
        name=request.POST.get('name')
        gender=request.POST.get('gender')
        score=request.POST.get('score')
        school=School.objects.get(id=request.POST.get('school'))
        print(name,gender,score,school)
        Student.objects.create(
            name=name,
            gender=gender,
            score=score,
            school=school
        )

        return redirect('/student/show/')

class Student_Update(View):
    def get(self,request,id):
        student_data=Student.objects.get(id=id)
        school_data=School.objects.all()
        return render(request, 'update.html', locals())

    def post(self,request,id):
        print(request.POST)
        name=request.POST.get('name'),
        gender=request.POST.get('gender'),
        score=request.POST.get('score'),
        school=School.objects.get(id=request.POST.get('school'))
        print(type(name))
        print(name,gender,score,school)
        Student.objects.filter(id=id).update(
            name=name,
            gender=gender,
            score=score,
            school=school
        )

        return redirect('/student/show/')


def student_info(request,id):
    student_data=Student.objects.get(id=id)
    return render(request,'info.html',locals())


def student_delete(request,id):
    Student.objects.get(id=id).delete()
    return redirect('/student/show/')


def setcookie(request):
    response=HttpResponse('设置cookie ')

    endtime=datetime.datetime.now()+datetime.timedelta(hours=2)
    response.set_cookie('mycookie','zhangsanerer',expires=endtime)
    return response
def del_set_cookie(request):
    response=HttpResponse('删除cookie')
    response.delete_cookie('mycookie')
    return response
def setsession(request):
    request.session['id']=666
    return HttpResponse('设置session')

def session_del(request):

    if request.session.get('id'):

        # del request.session['id']
        request.session.flush()
        return HttpResponse('Session被删了')
    else:
        return HttpResponse('目前没有任何需要删除的session')




